package org.example.uuadb.util;

import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @description 日期时间工具类
 *
 * @author CY 
 * @date 2017-1-12
 */
@Slf4j
public class DateTimeUtil {

	//数据库时间格式
	private static final SimpleDateFormat sqlDateFormatter;
	//年月日日期格式
	private static final SimpleDateFormat ymdDateFormatter;
	static {
		sqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//"2017-13-17 00:00:00"这种输入要报错
		sqlDateFormatter.setLenient(false);
		ymdDateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		//"2017-13-17"这种输入要报错
		ymdDateFormatter.setLenient(false);
	}
	
	private DateTimeUtil() {
	}
	
	/**
	 * 将YYYY-MM-DD HH24:MI:SS格式的字符串转换为日期
	 * @param dateStr YYYY-MM-DD HH24:MI:SS格式字符串
	 * @return 日期
	 */
	public static Date sqlDateStr2Date(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		Date date = null;
		try {
			date = sqlDateFormatter.parse(dateStr);
		} catch (ParseException e) {
			log.error("Convert String to Date ERROR:", e);
		}
		return date;
	}
	
	/**
	 * 将日期转换为YYYY-MM-DD HH24:MI:SS格式的字符串
	 * @param date 日期
	 * @return YYYY-MM-DD HH24:MI:SS格式的字符串
	 */
	public static String sqlDate2DateStr(Date date) {
		if (date == null) {
			date = new Date();
		}
		return sqlDateFormatter.format(date);
	}
	
	/**
	 * 将日期转换为YYYY-MM-DD格式的字符串
	 * @param date 日期
	 * @return YYYY-MM-DD格式的字符串
	 */
	public static String date2YMDDateStr(Date date) {
		if (date == null) {
			date = new Date();
		}
		return ymdDateFormatter.format(date);
	}
	/**
	 * 将YYYY-MM-DD格式的字符串转换为日期
	 * @param dateStr YYYY-MM-DD格式字符串
	 * @return 日期
	 */
	public static Date YMDDateStr2Date(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		Date date = null;
		try {
			date = ymdDateFormatter.parse(dateStr);
		} catch (ParseException e) {
			log.error("Convert String to Date ERROR:", e);
		}
		return date;
	}
	/**
	 * 将日期增加固定天数获取新日期
	 * @param date		日期
	 * @param years		增加的年
	 * @param months	增加的月
	 * @param days		增加的日
	 * @return
	 */
	public static Date getNewDatePlusDays(Date date,int years, int months,int days){
		if(date == null){
			date = new Date();
		}
		
		Calendar cl=Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.YEAR, years);
		cl.add(Calendar.MONTH, months);
		cl.add(Calendar.DATE,days);
		
		return cl.getTime();
	}
	
	/**
	 * 将日期增加固定天数获取新日期
	 * @param date		日期
	 * @param years		增加的年
	 * @param months	增加的月
	 * @param days		增加的日
	 * @param hours		增加的时
	 * @param minutes	增加的分
	 * @param seconds	增加的秒
	 * @return
	 */
	public static Date getNewDatePlusDays(Date date,int years, int months, int days, int hours, int minutes,int seconds){
		if(date == null){
			date = new Date();
		}
		
		Calendar cl=Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.YEAR, years);
		cl.add(Calendar.MONTH, months);
		cl.add(Calendar.DATE,days);
		cl.add(Calendar.HOUR, hours);
		cl.add(Calendar.MINUTE, minutes);
		cl.add(Calendar.SECOND,seconds);
		return cl.getTime();
	}
	
	/**
	 * 计算时间差，如相差几年、几个月、几天
	 * @param  	startTime	开始时间
	 * @param  	endTime 	结束时间
	 * @param	timeFormat	startTime/endTime字符串的表示格式，如 "yyyy-MM-dd HH:mm:ss"或 "yyyy-MM-dd"
	 * @param  	methodType 	统计类型，如day计算相差的天数、month计算相差的月数、year计算相差的年数
	 * @return	 时间差
	 */
	public static int getCountByTimeInterval(String startTime,String endTime,String timeFormat, String methodType){
		int count = 0;
		
		/*将startTime和endTime字符串转化为时间Date*/
		SimpleDateFormat df = new SimpleDateFormat(timeFormat);
		Date startDate = new Date();
		Date endDate = new Date();
		try {
			startDate = df.parse(startTime);
			endDate = df.parse(endTime);
		} catch (ParseException e) {
			log.error("Convert Date ERROR:", e);
		}

		/*计算相差的天、或月、或年*/
		Calendar cal = Calendar.getInstance();
		if(methodType.equals("day")){
			cal.setTime(startDate);
			int startDay = getSumOfDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
			cal.setTime(endDate);
			int endDay = getSumOfDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH));
			int daysCount = endDay-startDay+1;
			count = daysCount;
		} else if(methodType.equals("month")){
			cal.setTime(endDate);
			int endYear = cal.get(Calendar.YEAR);
			int endMonth = cal.get(Calendar.MONTH);
			cal.setTime(startDate);
			int startYear = cal.get(Calendar.YEAR);
			int startMonth = cal.get(Calendar.MONTH);
			int monthsCount =12*(endYear-startYear)+endMonth-startMonth+1;
			count = monthsCount;
		} else if(methodType.equals("year")){
			cal.setTime(endDate);
			int endYear = cal.get(Calendar.YEAR);
			cal.setTime(startDate);
			int startYear = cal.get(Calendar.YEAR);
			int yearsCount=endYear-startYear+1;
			count = yearsCount;
		}
		return count;
	}


	public static String calculateEndTime(String searchDateType) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_1 = Calendar.getInstance();
		cal_1.clear();
		String startDate;
		switch (searchDateType) {
			case "TODAY":
				//查询当天的数据
				cal_1.setTime(new Date());
				cal_1.add(Calendar.DAY_OF_MONTH, -1);
				startDate = df.format(cal_1.getTime()) + " 00:00:00";
				break;
			case "WEEK":
				//查询最近一周的数据
				cal_1.setTime(new Date());
				cal_1.add(Calendar.DAY_OF_MONTH, -7);
				startDate = df.format(cal_1.getTime()) + " 00:00:00";
				break;
			case "MONTH":
				//查询最近一月的数据
				cal_1.setTime(new Date());
				cal_1.add(Calendar.DAY_OF_MONTH, -30);
				startDate = df.format(cal_1.getTime()) + " 00:00:00";
				break;
			case "TOTAL":
				//按年统计
				startDate = "";
				break;
			default:
				startDate = "";
				break;
		}
		return startDate;
	}
	
	/**
	 * 计算一个日期的大天数
	 * @param y	年
	 * @param m	月
	 * @param d	日
	 * @return
	 */
	private static int getSumOfDate(int y, int m, int d){
		int[] x={0,31,28,31,30,31,30,31,31,30,31,30,31};
		int i,s=0;
		for (i=1;i<y;i++){
			if( i%4==0  && ( i%100!=0 || i%400==0)){
				s+=366;
			} else {
				s+=365;
			}
		}
		if(y%4==0 && ( y%100!=0 || y%400==0)){
			x[2]=29;
		}
		for (i=1;i<m;i++){
			s+=x[i];
		}
		s+=d;
		return s;
	}
	
	/**
	 * 若海事手续中的时间为无效值1900时，则返回位空，让前台显示为空
	 * @param
	 * @return
	 */
	public static String removeUnvalidTime(String datetimeStr){
		String datetimeStrDeal = "";
		if((datetimeStr!=null) && (datetimeStr.length()>10)
				&& (datetimeStr.substring(0, 4).equals("1900"))){
			datetimeStrDeal = "";
		}else{
			datetimeStrDeal = datetimeStr;
		}
		return datetimeStrDeal;
	}

	public static String utcToLocal(Date utcTime){
		String utcTimeStr = sqlDateFormatter.format(utcTime);
		SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
		utcFormater.setLenient(false);
		Date gpsUtcDate = null;
		try {
			gpsUtcDate = utcFormater.parse(utcTimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return utcTimeStr;
		}
		SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		localFormater.setTimeZone(TimeZone.getDefault());
		localFormater.setLenient(false);
		return localFormater.format(gpsUtcDate.getTime());
	}

	public static Long getTodayZeroTime() {
		long current = System.currentTimeMillis();
		return current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
	}

	/**
	 * 生成17位时间戳
	 * @return 17位时间戳
	 */
	public static String getTime17() {
		return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
	}

	public static String getCurrentTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

}
